package com.furns.filter;

import com.furns.utils.JDBCUtilsByDruid;

import javax.servlet.*;
import java.io.IOException;

/**
 * 管理事务
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        try {
            filterChain.doFilter(servletRequest,servletResponse);
            JDBCUtilsByDruid.commit();
        } catch (Exception e) {
            JDBCUtilsByDruid.rollback();
//            e.printStackTrace();
            //由于filter是由tomcat创建，异常抛给服务器，再在web.xml文件中找到error-page
            throw new RuntimeException(e);
        }
    }

    @Override
    public void destroy() {

    }
}
